import { ITodoData } from "../types/typings";
import TodoDom from "./TodoDom";

// 操作数据的类
class TodoEvent extends TodoDom {
    private todoData: ITodoData[];
    constructor(todoData: ITodoData[], todoWrapper: HTMLElement) {
        super(todoWrapper);
        this.todoData = todoData;
        this.initList(this.todoData);
    }
    public addTodo(todo: ITodoData): undefined | number {
        // 去重 不能重复添加
        const _todo = this.todoData.filter((item: ITodoData) => item.content === todo.content);
        if (_todo.length < 1) {
            this.todoData.push(todo);
            this.addItem(todo);
            return;
        }
        return 1001;
    }
    public removeTodo(target: HTMLElement, id: number): void {
        this.todoData = this.todoData.filter((item: ITodoData) => item.id !== id);
        this.removeItem(target);
    }
    public toggleCompleted(target: HTMLElement, id: number): void {
        this.todoData = this.todoData.map((item: ITodoData) => {
            if (item.id === id) {
                item.completed = !item.completed;
                this.changeCompleted(target, item.completed);
            }
            return item;
        })
    }
}
export default TodoEvent;